#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Web;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlManageConfirmBooking : UserControl
    {
        User m_current_user;
        CheckInService m_BookingService;
        Keys m_currentKey;
        MainForm m_mainForm;
        TravelAgentService m_travelAgentService;
        SeaPortService m_SeaPortService;
        TripService m_tripService;
        //EditMode m_editmode;

        public UserControlManageConfirmBooking(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            m_current_user = currentUser;
            m_BookingService = (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            m_SeaPortService = (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_tripService = (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_travelAgentService = (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            m_mainForm = mainForm;
            setPermission();
            textBoxSearch.SelectAll();
            LoadAllBooking();
            initToolBarButtonClick();
            enableButton(false);
        }


        private void initToolBarButtonClick()
        {
           // toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonConfirm.Click += new EventHandler(toolBarButtonConfirmZ_Click);
            toolBarButtonCloseBooking.Click += new EventHandler(toolBarButtonCloseBooking_Click);
            //toolBarButtonView.Click += new EventHandler(toolBarButtonView_Click);
        }

        void toolBarButtonCloseBooking_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            MessageBox.Show("Are you sure to close this bookin ? " + user.m_bookingCode, "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, new EventHandler(ClosedConfirmation));
        }

        //void toolBarButtonView_Click(object sender, EventArgs e)
        //{
        //    loadUserForm(EditMode.Load);
        //}

        void toolBarButtonConfirmZ_Click(object sender, EventArgs e)
        {
            loadUserForm(EditMode.Edit);
        }

        void enableButton(bool enable)
        {
            //toolBarButtonDelete.Enabled = enable && m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonConfirm.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag;
            //toolBarButtonView.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
            toolBarButtonCloseBooking.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag;
            if (listViewRecords.SelectedItem != null)
            {
                CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
                toolBarButtonCloseBooking.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag && user.m_bookingStatus != CheckInStatus.Closed;
            }
        }

        private void setPermission()
        {
           // toolBarButtonDelete.Enabled = m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonConfirm.Enabled = m_current_user.RoleDetails(this.Name).UpdateFlag;
           // toolBarButtonClear.Enabled = m_current_user.RoleDetails(this.Name).NewFlag;
           // toolBarButtonView.Enabled = m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            LoadAllBooking();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            //if (listViewRecords.SelectedItem == null) return;
            //if (listViewRecords.SelectedItem.Tag == null) return;
            //MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
            //    MessageBoxIcon.Question, new EventHandler(ClosedConfirmation));
        }

        void ClosedConfirmation(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
                    if (user.m_bookingID > 0)
                    {
                        m_BookingService.UpdateCheckInStatus(user, CheckInStatus.Closed);
                        MessageBox.Show("Book has been closed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        // clearForm();
                        //LoadAllBooking();
                        //setEditMode(EditMode.Deleted);
                        //listViewRecords.Items.RemoveAt(listViewRecords.SelectedItem.Index);
                        listViewRecords.SelectedItem.SubItems[9].Text = CheckInStatus.Closed.ToString();
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private bool CheckPassengerBookStatus(CheckIn user)
        {
            bool confirm = false;
            foreach (Passenger pss in user.m_passengerList)
            {
                if (pss.m_passengerStatus == PassengerStatus.Boarding)
                {
                    confirm = true;
                    break;
                }
            }
            return confirm;
        }

        void toolBarButtonClear_Click(object sender, EventArgs e)
        {
            ConfirmBookingForm frm = new ConfirmBookingForm(this.Name, new CheckIn(), m_current_user, m_mainForm, this, EditMode.Create);
            frm.ShowDialog();

        }

        public void LoadAllBooking()
        {
            listViewRecords.Items.Clear();
            IList users = m_current_user.UserType == UserType.Administrators ? m_BookingService.LoadAllCheckIn() : m_BookingService.LoadAllCheckInByTravelAgent(m_current_user.TravelAgent.TravelAgentID);
            foreach (CheckIn booking in users)
            {
                booking.m_trip = m_tripService.GetTripByID(booking.m_trip.m_tripID);
                booking.m_trip.m_seaPort = m_SeaPortService.GetSeaPortByID(booking.m_trip.m_seaPort.m_seaPortID);
                booking.m_travelAgent = m_travelAgentService.GetTravelAgentByID(booking.m_travelAgent.TravelAgentID);

                ListViewItem item = listViewRecords.Items.Add(booking.m_bookingCode);
                if (booking.m_bookingStatus == CheckInStatus.ReCeckIn)
                    item.ForeColor = Color.Red;
                item.SubItems.Add(booking.m_bookingStatus.ToString());
                item.SubItems.Add(booking.m_travelAgent.Name);
                item.SubItems.Add(booking.m_groupName);
                item.SubItems.Add(booking.m_bookingDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(booking.m_bookNumber.ToString());
                item.SubItems.Add(booking.m_ticketType.ToString());
                item.SubItems.Add(booking.m_trip.m_seaPort.m_seaPortName);
                item.SubItems.Add(booking.m_trip.m_tripCode);
                item.SubItems.Add(booking.m_trip.m_departureDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(booking.m_trip.m_departureTime.ToString("HH:mm"));

                item.Tag = booking;
            }
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = new ArrayList();// m_current_user.UserType == UserType.Administrators ? m_BookingService.SearchBookingByCodeName(textBoxSearch.Text) : m_BookingService.SearchBookingByCodeNameAndTravelAgent(textBoxSearch.Text, m_current_user.TravelAgent.TravelAgentID, datet);
            foreach (CheckIn booking in users)
            {
                booking.m_trip = m_tripService.GetTripByID(booking.m_trip.m_tripID);
                booking.m_trip.m_seaPort = m_SeaPortService.GetSeaPortByID(booking.m_trip.m_seaPort.m_seaPortID);
                booking.m_travelAgent = m_travelAgentService.GetTravelAgentByID(booking.m_travelAgent.TravelAgentID);
                ListViewItem item = listViewRecords.Items.Add(booking.m_bookingCode);
                if (booking.m_bookingStatus == CheckInStatus.ReCeckIn)
                    item.ForeColor = Color.Red;
                item.SubItems.Add(booking.m_bookingStatus.ToString());
                item.SubItems.Add(booking.m_travelAgent.Name);
                item.SubItems.Add(booking.m_groupName);
                item.SubItems.Add(booking.m_bookingDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(booking.m_bookNumber.ToString());
                item.SubItems.Add(booking.m_ticketType.ToString());
                item.SubItems.Add(booking.m_trip.m_seaPort.m_seaPortName);
                item.SubItems.Add(booking.m_trip.m_tripCode);
                item.SubItems.Add(booking.m_trip.m_departureDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(booking.m_trip.m_departureTime.ToString("HH:mm"));
                item.Tag = booking;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0) 
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
                textBoxSearch.SelectAll(); 
            }
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            //if (toolBarButtonEdit.Enabled)
            //    loadUserForm(EditMode.Edit);
            //else
            if (!toolBarButtonConfirm.Enabled) return;
            loadUserForm(EditMode.Edit);
        }

        private void loadUserForm( EditMode editmode )
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            ConfirmBookingForm frm = new ConfirmBookingForm(this.Name, user, m_current_user, m_mainForm, this, editmode);
            frm.ShowDialog();
        }

        private void textBox_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }


        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void listViewRecords_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                listViewRecords_DoubleClick(objSender, null);
            }
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            textBoxSearch.SelectAll();
        }

        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            enableButton(listViewRecords.SelectedItem != null);            
            if (listViewRecords.SelectedItem != null)
                enableButton(listViewRecords.SelectedItem.Tag != null);
        }

    }
}